package com.example.shiroplusdemo.controller;

import com.alibaba.fastjson.JSON;
import com.example.shiroplusdemo.entity.CustomPage;
import com.example.shiroplusdemo.entity.FrontPage;
import com.example.shiroplusdemo.entity.UserOnlineVO;
import com.example.shiroplusdemo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-01-12
 **/
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 在线用户列表
     * @return
     */
    @RequestMapping("/onlineUserPage")
    @ResponseBody
    public CustomPage<UserOnlineVO> OnlineUsers(FrontPage frontPage){
        Page<UserOnlineVO> onlineVOPage = userService.getUserSessionPage(frontPage);
        CustomPage<UserOnlineVO> customOnlineUserPage = new CustomPage<>(onlineVOPage,frontPage);
        return customOnlineUserPage;
    }


    /**
     * 强制踢出用户
     */
    @RequestMapping("/kickout")
    @ResponseBody
    public Map<String,Object> kickout(@RequestParam("ids[]")String[] sessionIds){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            for(String sessionId:sessionIds){
                userService.kickout(sessionId);
            }
            resultMap.put("flag",true);
            resultMap.put("msg","强制踢出成功");
        }catch(Exception e) {
            resultMap.put("flag",false);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }



}
